package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.School;
import cn.wolfcode.crm.mapper.SchoolMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.ISchoolService;
import cn.wolfcode.crm.util.PageResult;
import org.apache.poi.hssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2018/6/12.
 */
@Service
public class SchoolServiceImpl implements ISchoolService{
    @Autowired
    private SchoolMapper schoolMapper;
    @Override
    public void insert(School record) {
        schoolMapper.insert(record);
    }

    @Override
    public void updateByPrimaryKey(School record) {
        schoolMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<School> selectAll() {
        return schoolMapper.selectAll();
    }

    @Override
    public PageResult query(QueryObject qo) {
        int total = schoolMapper.query4Count(qo);
        if (total > 0) {
            return new PageResult(total, schoolMapper.query4List(qo));
        }
        return new PageResult(total, Collections.EMPTY_LIST);
    }

    @Override
    public int deleteByPrimaryKey(Long id) {

        return schoolMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void changState(Long id) {
        schoolMapper.changState(id);
    }


    public HSSFWorkbook exportXls(QueryObject qo) {
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet("school");
        HSSFRow row = sheet.createRow(0);
        row.createCell(0).setCellValue("编号");
        row.createCell(1).setCellValue("学校");
        row.createCell(2).setCellValue("地址");
        row.createCell(3).setCellValue("重要度");
        row.createCell(4).setCellValue("意向度");
        row.createCell(5).setCellValue("学校电话");
        row.createCell(6).setCellValue("意向学科");
        row.createCell(7).setCellValue("联系人");
        row.createCell(8).setCellValue("营销人员");
        row.createCell(9).setCellValue("跟进人员");
        row.createCell(10).setCellValue("上次跟进时间");
        row.createCell(11).setCellValue("下次跟进时间");
        row.createCell(12).setCellValue("下次跟进时间");
        row.createCell(13).setCellValue("跟进状态");
        row.createCell(14).setCellValue("客户状态");
        List<School> schools = schoolMapper.selectXls(qo);
        if (schools.size() > 0) {
            for (int i = 0; i < schools.size(); i++) {
                School school = schools.get(i);
                row = sheet.createRow(i + 1);
                row.createCell(0).setCellValue(school.getId());
                row.createCell(1).setCellValue(school.getSchool());
                row.createCell(2).setCellValue(school.getSite());
                row.createCell(3).setCellValue(school.getImportance());
                row.createCell(4).setCellValue(school.getIntentionDegree());
                row.createCell(5).setCellValue(school.getPhone());
                row.createCell(6).setCellValue(school.getIntentionSubject());
                row.createCell(7).setCellValue(school.getLinkmanContacts().getName());
                row.createCell(8).setCellValue(school.getMarketingPersonnel());
                row.createCell(9).setCellValue(school.getFollowStaff());
                row.createCell(10).setCellValue(school.getLastFollowTime());
                row.createCell(11).setCellValue(school.getNextFollowTime());
                row.createCell(12).setCellValue(school.getFollowState());
                row.createCell(13).setCellValue(school.getServiceState());
                //设置导出的日期的格式
                HSSFCell cell = row.createCell(5);
                cell.setCellValue(new Date());
                HSSFCellStyle cellStyle = workbook.createCellStyle();

                HSSFDataFormat format = workbook.createDataFormat();

                cellStyle.setDataFormat(format.getFormat("yyyy年m月d日"));

                cell.setCellStyle(cellStyle);

            }
        }
        return workbook;
    }

    public Long selectById(String schoolName) {
        return schoolMapper.selectById(schoolName);
    }


}
